package com.sc.nft.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sc.nft.dao.DaoShareConfigDao;
import com.sc.nft.dao.DigitalCollectionDao;
import com.sc.nft.dao.UserCreatorInfoDao;
import com.sc.nft.dto.profit.SaveSharePoolDTO;
import com.sc.nft.entity.DaoShareConfig;
import com.sc.nft.entity.vo.DaoShareConfigVO;
import com.sc.nft.entity.vo.GetDaoByIdVO;
import com.sc.nft.enums.AwardsTypeEnum;
import com.sc.nft.exception.GlobalRunTimeException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import com.sc.nft.service.DaoShareConfigService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * 分红配置表服务接口实现
 *
 * @author zxy
 * @description 由 Mybatisplus Code Generator 创建
 * @since 2023-12-14 16:20:11
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class DaoShareConfigServiceImpl implements DaoShareConfigService {
    private final DaoShareConfigDao daoShareConfigDao;
    private final UserCreatorInfoDao userCreatorInfoDao;
    private final DigitalCollectionDao digitalCollectionDao;

    @Override
    public DaoShareConfig getById(Long id) {
        return daoShareConfigDao.getById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveConfig(SaveSharePoolDTO dto) {
        verifySaveConfig(dto);
        DaoShareConfig daoShareConfig = null;
        if (ObjectUtil.isNotNull(dto.getId())) {
            daoShareConfig = daoShareConfigDao.getById(dto.getId());
            Assert.notNull(daoShareConfig, () -> new GlobalRunTimeException("未找到历史记录"));
        } else {
            daoShareConfig = new DaoShareConfig();
        }
        daoShareConfig.setName(dto.getName());
        daoShareConfig.setDaoId(dto.getDaoId());
        daoShareConfig.setAwardsType(dto.getAwardsType());
        daoShareConfig.setIdentityCollectionId(dto.getIdentityCollectionId());
        daoShareConfig.setPushCollectionId(dto.getPushCollectionId());
        daoShareConfig.setHoldUserCount(dto.getHoldUserCount());
        daoShareConfig.setCalDuration(dto.getCalDuration());
        daoShareConfig.setFirstShareRatio(dto.getFirstShareRatio());
        daoShareConfig.setSecondShareRatio(dto.getSecondShareRatio());
        Assert.isTrue(daoShareConfig.insertOrUpdate(), () -> new GlobalRunTimeException("当前页面已被修改，请刷新后重新提交"));
    }

    @Override
    public Page<DaoShareConfigVO> getConfigPage(Long daoId, String name, Boolean status, Page page) {
        return daoShareConfigDao.getConfigPage(daoId, name, status, page);
    }

    @Override
    public List<DaoShareConfig> getOpenByDaoId(Long daoId) {
        return daoShareConfigDao.getOpenByDaoId(daoId);
    }

    private void verifySaveConfig(SaveSharePoolDTO dto) {
        GetDaoByIdVO daoVO = userCreatorInfoDao.getDaoById(dto.getDaoId());
        Assert.notNull(daoVO, () -> new GlobalRunTimeException("DAO不存在"));
        Assert.isTrue(daoVO.getStatus().equals(1), () -> new GlobalRunTimeException("该品牌不是DAO"));
        Assert.isNull(daoShareConfigDao.getByNameExceptId(dto.getName(), dto.getId()), () -> new GlobalRunTimeException("分润池名称已存在"));
        if (dto.getAwardsType() != AwardsTypeEnum.POSITION_REWARD) {
            Assert.isNull(daoShareConfigDao.getByDaoIdAndCollectionIdAndTypeExceptId(dto.getDaoId(), dto.getIdentityCollectionId(), dto.getAwardsType(), dto.getId()), () -> new GlobalRunTimeException("该DAO下已存在此奖项"));
        }
        switch (dto.getAwardsType()) {
            case CULTIVATION_AWARD:
                Assert.notNull(digitalCollectionDao.getById(dto.getIdentityCollectionId()), () -> new GlobalRunTimeException("培育奖分润身份藏品不存在"));
                Assert.notNull(digitalCollectionDao.getById(dto.getPushCollectionId()), () -> new GlobalRunTimeException("培育奖分润直推藏品不存在"));
                Assert.isTrue(dto.getHoldUserCount() != null && dto.getHoldUserCount() > 0, () -> new GlobalRunTimeException("培育奖持有人数不正确"));
                break;
            case CONTRIBUTION_AWARD:
                Assert.notNull(digitalCollectionDao.getById(dto.getIdentityCollectionId()), () -> new GlobalRunTimeException("组织贡献奖分润身份藏品不存在"));
                Assert.notNull(digitalCollectionDao.getById(dto.getPushCollectionId()), () -> new GlobalRunTimeException("组织贡献奖分润直推藏品不存在"));
                Assert.isTrue(dto.getHoldUserCount() != null && dto.getHoldUserCount() > 0, () -> new GlobalRunTimeException("组织贡献持有人数不正确"));
                Assert.isTrue(dto.getCalDuration() != null && dto.getHoldUserCount() > 0, () -> new GlobalRunTimeException("统计时长不正确"));
                break;
            case SERVICE_CENTER_DIVIDENDS:
                if (dto.getDaoId() != 6L) {
                    throw new GlobalRunTimeException("非玛特DAO不允许创建该类型分润标准");
                }
                Assert.isTrue(dto.getCalDuration() != null, () -> new GlobalRunTimeException("统计时长不正确"));
                break;
            case SERVICE_CENTER_LEVEL:
                if (dto.getDaoId() != 6L) {
                    throw new GlobalRunTimeException("非玛特DAO不允许创建该类型分润标准");
                }
                break;
            case WORK_ROOM_DIVIDENDS:
                if (dto.getDaoId() != 6L) {
                    throw new GlobalRunTimeException("非玛特DAO不允许创建该类型分润标准");
                }
                break;
            case POSITION_REWARD:
                if (Objects.isNull(dto.getId())){
                    DaoShareConfig dsc = daoShareConfigDao.getByDaoIdAndCollectionIdAndTypeExceptId(dto.getDaoId(), dto.getIdentityCollectionId(), AwardsTypeEnum.POSITION_REWARD, null);
                    Assert.isNull(dsc, () -> new GlobalRunTimeException("该藏品已绑定持仓奖励"));
                }
                Assert.notNull(digitalCollectionDao.getById(dto.getIdentityCollectionId()), () -> new GlobalRunTimeException("持仓奖分润身份藏品不存在"));
                Assert.notNull(digitalCollectionDao.getById(dto.getPushCollectionId()), () -> new GlobalRunTimeException("持仓奖分润直推藏品不存在"));
                break;
            default:
                throw new GlobalRunTimeException("奖项类型不正确");
        }

    }


}